package com.liyao.protobuf.test.service;

import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.stub.StreamObserver;

import java.util.Iterator;

public class RpcClient {
    private static ManagedChannel channel = ManagedChannelBuilder
            .forAddress("127.0.0.1", RpcServer.port)
            .usePlaintext()
            .build();
 
    private static MyServiceGrpc.MyServiceBlockingStub blockingStub = MyServiceGrpc.newBlockingStub(channel);
    private static MyServiceGrpc.MyServiceStub asyncStub = MyServiceGrpc.newStub(channel);
 
    private static final StreamObserver<MyResponse> responseObserver = new StreamObserver<MyResponse>() {
        @Override
        public void onNext(MyResponse response) {
            System.out.println("receive: " + response.getValue());
        }
 
        @Override
        public void onError(Throwable t) {
            System.out.println("error");
        }
 
        @Override
        public void onCompleted() {
            System.out.println("completed");
        }
    };
 
    public static void main(String[] args) throws InterruptedException {
        //simpleSync();
        simpleAsync();
        //serverStreamSync();
        //serverStreamAsync();
        //clientStream();
       // biStream();
 
        Thread.sleep(100000);
    }
 
    private static void simpleSync() {
        MyRequest request = MyRequest.newBuilder()
                .addKeys(1)
                .build();
        String value = blockingStub.getByKey(request).getValue();
        System.out.println(value);
    }
 
    private static void simpleAsync() {
        MyRequest request = MyRequest.newBuilder()
                .addKeys(1)
                .build();
        asyncStub.getByKey(request, responseObserver);
    }
 
    private static void serverStreamSync() {
        MyRequest request = MyRequest.newBuilder()
                .addKeys(1)
                .addKeys(2)
                .addKeys(3)
                .build();
        Iterator<MyResponse> itr = blockingStub.getByKeyServerStream(request);
        while (itr.hasNext()) {
            System.out.println(itr.next());
        }
    }
 
    private static void serverStreamAsync() {
        MyRequest request = MyRequest.newBuilder()
                .addKeys(1)
                .addKeys(2)
                .addKeys(3)
                .build();
        asyncStub.getByKeyServerStream(request, responseObserver);
    }
 
    private static void clientStream() {
        StreamObserver<MyRequest> requestData = asyncStub.getByKeyClientStream(responseObserver);
        for (int i = 1; i <= 5; i++) {
            requestData.onNext(MyRequest.newBuilder().addKeys(i).build());
        }
        requestData.onCompleted();
    }
 
    private static void biStream() {
        StreamObserver<MyRequest> requestData = asyncStub.getByKeyBiStream(responseObserver);
        for (int i = 1; i <= 5; i++) {
            requestData.onNext(MyRequest.newBuilder().addKeys(i).build());
        }
        requestData.onCompleted();
    }
}